﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThietBiSo.LinQDB;
using ThietBiSo.Models;
using ThietBiSo.UrlReview;

namespace ThietBiSo.Controllers
{
    public class tintucController : ApplicationController
    {

        private TinTuc tt = new TinTuc();
        private MTinTuc mtt = new MTinTuc();

        MLoaiTinTuc mltt = new MLoaiTinTuc();
        LoaiTinTuc ltt = new LoaiTinTuc();
        //
        // GET: /TinTuc/

        public ActionResult Index(int? id)
        {
            int pageString = id ?? 0;
            int page = pageString == 0 ? 1 : pageString;
            int total = mtt.GetTotal();
            PagingController("/tintuc/index/", total, page, "");
            ViewData["tintucs"] = mtt.GetList(start, offset);
            return View();
        }
        public ActionResult ttuc(int id)
        {
            ViewData["maloaitintuc"] = id;
            ViewData["title"] = mltt.GetTenLoaiTin(id);
            //ViewData["tintucs"] = mtt.FindAll_HienThi(id);
            return View();
        }
        public ActionResult tintuc(int? id)
        {
          
            int pageString = id ?? 0;
            int page = pageString == 0 ? 1 : pageString;
           
        
          

            int total = mtt.GetTotalTinTuc();
           // string param = "?cbNhom=" + nhom;
            PagingController("/tintuc/tintuc/", total, page, "");

           


            List<TinTuc> lstTinTuc = new List<TinTuc>();
            lstTinTuc = mtt.GetList(start, offset);
          
          
            ViewData["tintucs"] = lstTinTuc;
            return View();
        }
        public ActionResult chitiet(string id)
        {
            SEOUrl s = new SEOUrl();
            ViewData["chitiets"] = mtt.ChiTiet(int.Parse(s.CatChuoi(id)));
            ViewData["title"] = mtt.GetTitleByMa(int.Parse(s.CatChuoi(id)));
            return View();
        }
        //
        // GET: /TinTuc/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /TinTuc/Create

        public ActionResult Create()
        {
           // string urlcbNhom = Request.QueryString["cbNhom"];
           // ViewData["tenloaitins"] = mltt.GetTenLoaiTin(int.Parse(urlcbNhom));
            //ViewData["urlcbNhom"] = urlcbNhom;
            return View();
        }

        //
        // POST: /TinTuc/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                //  string urlcbNhom = Request.QueryString["cbNhom"];
               // string urlcbNhom = collection.Get("cbNhom");
                tt.MaTinTuc = mtt.GetMax() + 1;
                tt.TieuDe = collection.Get("TieuDe");
               // tt.MaLoaiTinTuc = int.Parse(urlcbNhom);
                tt.NoiDungTomTat = collection.Get("NoiDungTomTat");
                tt.ChiTiet = System.Net.WebUtility.HtmlDecode(collection.Get("ChiTiet"));
                tt.TinMoi = true;
                tt.HienThi = true;
                tt.UrlHinh = collection.Get("HinhAnh");
                if (mtt.GetMaxTT() == 0 || mtt.GetTotalTinTuc() == 0)
                {
                    tt.ThuTu = 1;
                }
                else
                    tt.ThuTu = mtt.GetMaxTT() + 1;

                mtt.Save(tt);
                return Redirect("/tintuc/tintuc");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /TinTuc/Edit/5

        public ActionResult Edit(int id)
        {
            ViewData["tintucs"] = mtt.GetByID(id);
            //string urlcbNhom = Request.QueryString["cbNhom"];
            //ViewData["urlcbNhom"] = urlcbNhom;
            return View();
        }

        //
        // POST: /TinTuc/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                tt.MaTinTuc = id;
                //string urlcbNhom = collection.Get("cbNhom");
                tt.TieuDe = collection.Get("TieuDe");
                tt.NoiDungTomTat = collection.Get("NoiDungTomTat");

                if (collection.Get("HinhAnh") == null || collection.Get("HinhAnh") == "")
                {
                    tt.UrlHinh = collection.Get("HiddenHinhAnh");
                }
                else
                {
                    tt.UrlHinh = collection.Get("HinhAnh");
                }
             //   tt.MaLoaiTinTuc = int.Parse(collection.Get("ddlMaLoaiTT"));
                tt.ChiTiet = System.Net.WebUtility.HtmlDecode(collection.Get("ChiTiet"));
                tt.ThuTu = int.Parse(collection.Get("ThuTu"));
                mtt.Update(tt);
                return Redirect("/tintuc/tintuc");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /TinTuc/Delete/5

        public ActionResult Delete(int id)
        {
            mtt.Delete(id);
            return View();
        }

        //
        // POST: /TinTuc/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("tintuc");
            }
            catch
            {
                return View();
            }
        }
    }
}
